package com.colabox.server;


import java.io.IOException;





import javax.servlet.*;
import javax.servlet.http.*;

import com.google.gdata.client.GoogleService.CaptchaRequiredException;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.client.spreadsheet.SpreadsheetService;

import com.google.gdata.data.acl.AclEntry;
import com.google.gdata.data.acl.AclFeed;
import com.google.gdata.data.acl.AclRole;
import com.google.gdata.data.acl.AclScope;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.util.AuthenticationException;
import com.google.gdata.util.ServiceException;



/*
 * http://code.google.com/apis/documents/docs/2.0/developers_guide_java.html#AuthClientLogin
 * docs api
 * 
 * 
 * 
 */
public class testservlet extends HttpServlet {
	  private String captchaToken = null;
	  private SpreadsheetService service;
	 // private DocumentList documentList;
	  protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	  {
	      System.out.println("doget");
	      
	      response.setContentType("text/xml");

	      String firstName=request.getParameter("p0");
		  
	      System.out.println(firstName);
	      //processRequest(request,response,"GET");
	  }
	  
	  public void doPost(HttpServletRequest req, HttpServletResponse res)  
      throws ServletException, IOException {  
            
		    System.out.println("dopost");
		    authenticate("untosil@gmail.com","ihateself");
		  
		}  
	  
	  private boolean docslogin(){
	     // documentList = new DocumentList("colabox demo",  DocumentList.DEFAULT_AUTH_PROTOCOL, DocumentList.DEFAULT_AUTH_HOST, DocumentList.DEFAULT_PROTOCOL, DocumentList.DEFAULT_HOST);

		  
		  return true;
	  }
	  
	
	  private boolean authenticate(
		      String username, String password) {
		    try {
		    	System.out.println("0");
		    	captchaToken="12";
		    	service=new SpreadsheetService("SpreadsheetApiDemo-1");
		      if (captchaToken == null) {

		        // No CAPTCHA challenge was presented.
		        // Proceed to the next step.
		    	  System.out.println("1");
		        service.setUserCredentials(username, password);
		        System.out.println("2");
		       
		      } else {

		        // Use the CAPTCHA token and answer to help the
		        // authentication.
		        service.setUserCredentials(username, password, captchaToken,
		           "");
		      }
		      System.out.println("login sucess!");
		      return true;
		    } catch (Exception e) {

		      // Get the CAPTCHA token and display the image.
		    //  captchaToken = e.getCaptchaToken();
		      System.out.println("login error1!+e="+e.toString());
		      return false;
		    } 
		  }

}